$(document)
		.ready(
				function() {
					
					$("#editissuebtnsave")
					.click(
							function() {

								var htmltext = $("#issuedetails").val();
								//htmltext = htmlspecialchars(htmltext, &quot;);
								htmltext = htmltext.replaceAll('"', "&quot;");
								//var htmltext = CKEDITOR.instances.issuedetails.getData();
								// alert("Content: "+ htmltext);
								//alert("CAtegpru: "+ $("#category option:selected").text());
								$
										.ajax({
											type : "post",
											url : "../addissue?action=update&issueid="+ editissueid,
											datatype : "json",
											data : "issuetitle="
													+ $("#issuetitle")
															.val()
													+ "&bugid="
													+ $("#bugid").val()
													+ "&version="
													+ $("#version")
															.chosen()
															.val()
													+ "&moduleid="
													+ $("#module")
															.chosen()
															.val()
													+ "&module="
													+ $(
															"#module option:selected")
															.text()
													+ "&category="
													+ $(
															"#category option:selected")
															.text()
													+ "&subcategory="
													+ $(
															"#subcategory option:selected")
															.text()
													+ "&categoryid="
													+ $("#category")
															.chosen()
															.val()
													+ "&subcategoryid="
													+ $("#subcategory")
															.chosen()
															.val()
													+ "&issuedetails="
													+ escape(htmltext),
											success : function(data) {
												
													window.location = "viewissue.jsp?issueid="
															+ editissueid;
												
											},
											error : function(jqXHR,
													textStatus) {
												alert("Request failed: "
														+ textStatus);
											}
										});
							});
					
					

					

					$.getJSON('../GetVersions', function(data) {

						var i = 0;
						$("#version").html("<option> </option>");
						for (i = 0; i < data.versions.length; i++) {
							$("#version").append(
									'<option value="' + data.versions[i] + '">'
											+ data.versions[i] + '</option>');
							// alert("Version: "+data.versions[i] );
						}

						var ver = $("#hversion").val();
						var verArray = ver.split("  ,  ");

						for (i = 0; i < verArray.length; i++) {
							// alert(verArray[i]);
							//$("#version option:contains(" + verArray[i] + ")").attr('selected', 'selected');
							//$("#version option:equals(" + verArray[i] + ")").attr('selected', 'selected');
							$('#version').val(verArray);
						}

						// $("#version option:contains(" + "9.0.128" +
						// ")").attr('selected', 'selected');

						$("#version").trigger("liszt:updated");
						$("#version").chosen();

					});

					$("#module")
							.change(
									function() {
										// alert("You changed value to: " +
										// $("#module option:selected").text() +
										// " moduleid: " +
										// this.value.toString());
										var parentcategory;
										parentcategory = this.value;
										modulename = $(
												"#module option:selected")
												.text();
										moduleid = this.value.toString();
										categoryname = "0";
										categoryid = "0";
										subcategoryid = "0";
										subcategoryid = "0";
										
										$("#category")
										.html("<option> </option>");
										$('#category').trigger("liszt:updated");

										$
												.getJSON(
														'../GetCategories?parentid='
																+ parentcategory,
														function(data) {
															var i = 0;
															$("#category")
																	.html(
																			"<option value=\"\"> </option>");
															for (i = 0; i < data.modules.length; i++) {
																$("#category")
																		.append(
																				'<option value="'
																						+ data.modules[i].id
																						+ '">'
																						+ data.modules[i].sectionname
																						+ '</option>');
															}
															$('#category')
																	.trigger(
																			"liszt:updated");
														});
										$("#subcategory")
										.html(
												"<option> </option>");
										$('#subcategory')
										.trigger(
												"liszt:updated");

									});
					$("#category")
							.change(
									function() {
										// alert("You changed country to: " +
										// $("#module option:selected").text() +
										// " moduleid: " +
										// this.value.toString());
										var parentcategory;
										categoryname = $(
												"#category option:selected")
												.text();
										categoryid = this.value.toString();
										subcategoryid = "0";
										subcategoryid = "0";

										parentcategory = this.value;
										
										$("#subcategory")
										.html(
												"<option> </option>");
										$('#subcategory')
										.trigger(
												"liszt:updated");
										
										
										$
												.getJSON(
														'../GetCategories?parentid='
																+ parentcategory,
														function(data) {
															var i = 0;
															$("#subcategory")
																	.html(
																			"<option> </option>");
															for (i = 0; i < data.modules.length; i++) {
																$(
																		"#subcategory")
																		.append(
																				'<option value="'
																						+ data.modules[i].id
																						+ '">'
																						+ data.modules[i].sectionname
																						+ '</option>');
															}
															$('#subcategory')
																	.trigger(
																			"liszt:updated");
														});

									});

					$("#subcategory").change(
							function() {
								subcategoryname = $(
										"#subcategory option:selected").text();
								subcategoryid = this.value.toString();
							});

					$("#btncancel").click(function() {
						// window.location("home.jsp");
						window.location.assign("viewissue.jsp?issueid="+ editissueid);
					});

				});